package com.hystrix.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hystrix.model.InfoA;

@RestController
@RequestMapping(value = "/service/b")
public class ServiceBController {

	private static Map<String, Object> data = new HashMap<String, Object>();

	static {
		data.put("1", "Hello1");
		data.put("2", "Hello2");
		data.put("3", "Hello3");
		data.put("4", "Hello4");
	}

	@RequestMapping(value = "/info")
	public String getInfo() {
		InfoA info = new InfoA();
		info.setService("serviceB");
		info.setInfo("我是服务B");
		return JSONObject.toJSONString(info);
	}

	@RequestMapping(value = "/reactive")
	public String getColdData(String reactive) {
		System.out.println("请求过来了:" + reactive);
		return JSONObject.toJSONString(data);
	}

	@RequestMapping(value = "/cache")
	public String cache(String cacheId) {
		System.out.println("请求过来了:cacheId=" + cacheId);
		return JSONObject.toJSONString(data.get(cacheId));
	}

	@RequestMapping(value = "/batch")
	public String batch(String cacheIds) {
		System.out.println("请求过来了:batch=" + cacheIds);
		JSONArray array = new JSONArray();
		String[] ids = cacheIds.split(",");
		for (int i = 0; i < ids.length; i++) {
			JSONObject obj = new JSONObject();
			obj.put(ids[i], data.get(ids[i]));
			array.add(obj);
		}
		return JSONObject.toJSONString(array);
	}
}
